package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.bo.WxCouponExchangeBO;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.pojo.MarketCouponUser;
import com.cskaoyan.bean.pojo.MarketUser;
import com.cskaoyan.bean.vo.CouponListVO;
import com.cskaoyan.bean.vo.WxMyCouponListVO;
import com.cskaoyan.service.CouponService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 微信小程序的优惠券模块
 * 创建日期: 2022/04/10 23:09
 *
 * @author ChengWenqi
 */

@RestController
@RequestMapping("wx/coupon")
public class WxCouponController {
    @Autowired
    CouponService couponService;

    // 所有优惠券列表 wx/coupon/list（不用登录）
    @RequestMapping("list")
    public BaseRespVo couponList(BaseParam param) {
        CouponListVO couponListVO = couponService.queryCouponList(param);
        return BaseRespVo.ok(couponListVO);
    }

    // 我的优惠券列表 wx/coupon/mylist（需要登录）
    @RequestMapping("mylist")
    public BaseRespVo couponMyList(BaseParam param, short status) {
        MarketUser user = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        if (user == null) {
            return BaseRespVo.fail(501, "请登录");
        }
        WxMyCouponListVO wxMyCouponListVO = couponService.queryMyCouponList(param, status, user);
        return BaseRespVo.ok(wxMyCouponListVO);
    }


    // 下单时选择优惠券 wx/coupon/selectlist（需要登录）
    @RequestMapping("selectlist")
    public BaseRespVo couponSelectList(Integer cartId, Integer grouponRulesId) {
        MarketUser user = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        if (user == null) {
            return BaseRespVo.fail(501, "请登录");
        }
        if (cartId == null || grouponRulesId == null) {
            return BaseRespVo.fail(10000, "参数不合法");
        }

        WxMyCouponListVO wxMyCouponListVO = couponService.queryMySelectCouponList(cartId, user);
        return BaseRespVo.ok(wxMyCouponListVO);
    }


    // 领取优惠券 wx/coupon/receive（需要登录）
    @RequestMapping("receive")
    public BaseRespVo couponReceive(@RequestBody MarketCouponUser couponUser) {
        MarketUser user = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        if (user == null) {
            return BaseRespVo.fail(501, "请登录");
        }

        int code = couponService.couponReceive(couponUser, user);
        if (code == 200) {
            return BaseRespVo.ok();
        }
        if (code == 740) {
            return BaseRespVo.fail(740, "优惠券已经领取过");
        }
        return BaseRespVo.fail(10000, "优惠券已失效");
    }


    // 兑换优惠券 wx/coupon/exchange（需要登录）
    @RequestMapping("exchange")
    public BaseRespVo couponExchange(@RequestBody WxCouponExchangeBO bo) {
        MarketUser user = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        if (user == null) {
            return BaseRespVo.fail(501, "请登录");
        }

        String code = bo.getCode();
        if (code == null) {
            return BaseRespVo.fail(10000, "请输入兑换码");
        }

        int i = couponService.couponExchange(code, user);
        if (i == 200) {
            return BaseRespVo.ok();
        }
        if (i == 740) {
            return BaseRespVo.fail(740, "优惠券已兑换");
        }
        //i == 742 或 404
        return BaseRespVo.fail(742, "优惠券不正确");
    }
}
